<div class="tableinfo">
    <div class="tableHeader">
        <p>Tin nhắn đã gởi</p>
    </div>
    
    
    <table border ="0" cellspacing ="0" cellpadding ="0">
        <tr style="background: silver; font-weight: bold;">
            <?php
        $user = $_SESSION['userlogin'];
        if($_SESSION['vaitro']=="Quản Trị"){
            $linkshowtn="../admin/hienthi_tinnhan.php?id=";
            $linkxoatn="<td align='center'><a href='../controller/xuly_xoa_tinnhan.php?id=$idtinnhan'title='Xóa tin nhắn'><img src='../images/delete_icon.png' alt='Xóa thông báo' /></a></td>";
            $td="<td>Xoá</td>";
        }else{
            $linkshowtn="../view/hienthi_tinnhan.php?id="; 
            $linkxoatn="";
            $td="";
        }?>             
                     
            <td>Tiêu đề </td>
            <td>ID </td>
            <td >Người nhận</td>
            <td >ngày</td>
            <td>Tình trạng</td>
            <?php echo $td; ?>
               
        </tr>
        
        <?php
        
            $user = $_SESSION['userlogin']; 
            
         
            //so dong hien thi tren mot trang
            $hienthi = 10;
            //tinh tong so trang
            if(isset($_GET['page']) && (int)$_GET['page']){
                $page = $_GET['page'];
            }else{
                $sodong = "select id_NhanVien from tinnhangui where id_NhanVien='$user'";
                $kq=executeQuery($sodong);
                $kq_sodong =mysql_num_rows($kq);
                $so = $kq_sodong;
                if($so>$hienthi){
                    $page = ceil($so/$hienthi);
                }else{
                    $page = 1;
                }
            }
            //vitri bat dau hien thi
            $vitri = (isset($_GET['vitri'])&& (int)$_GET['vitri']) ? $_GET['vitri']:0;
        
                    
            $sql = "SELECT a.id,a.TieuDe,a.NhanVienNhan,c.HoTen,date_format(a.ngaygoi,'%d-%m-%Y') as ngaygoi,b.TinhTrang FROM  tinnhangui a inner join tinhtrangtinnhan b on a.MaTTTN = b.id  inner join chitietnhanvien c on a.NhanViennhan = c.id_NhanVien where a.id_NhanVien='$user'  ORDER BY  a.ngaygoi DESC limit $vitri,$hienthi";
            $dstinnhangui = executeQuery($sql);
            
            while ($row = mysql_fetch_assoc($dstinnhangui)){
               
            $idtinnhangui = $row['id'];
            echo " <tr>
         
            <td><a href='$linkshowtn $idtinnhangui' style='font-size: medium;'>".$row['TieuDe']."</a></td>
            <td>".$row['NhanVienNhan']."</td>
            <td>".$row['HoTen']."</td>
            <td>".$row['ngaygoi']."</td>
            <td>".$row['TinhTrang']."</td>
            $linkxoatn

            </tr>" ;

            }
            $_SESSION['tinnhan']="tingui";
        ?>
        

    </table>

        <ul class="phantrang">
            <?php
               //echo $so; 
              if($page >1){
                $sau = $vitri + $hienthi;
                $truoc = $vitri - $hienthi;
                $tranght= ($vitri/$hienthi)+1;

                //nut pre                
                if($tranght !=1){
                    echo "<li><a href='../admin/tinnhandagui.php?vitri=".$truoc."'>Trước</a></li>";
                }
                
                //so trang
                for($i=1; $i<=$page; $i++ ){
                    if($tranght != $i){
                        echo "<li><a href='../admin/tinnhandagui.php?vitri=".$hienthi*($i-1)."'>$i</a></li>"; 
                    }else{
                        echo "<li class='hientai'><a href=''>$i</a></li>";
                    } 
                }
                
                
                //nut next
                if($tranght != $page){
                    echo "<li><a href='../admin/tinnhandagui.php?vitri=".$sau."'>Sau</a></li>";
                }
              }  
            ?>
             
        </ul>     
</div>
